package main

import (
	"flag"
	"fmt"
	"os"
	"time"

	"file/internal/config"
	"file/internal/handler"
	"file/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var env string

func main() {
	flag.Parse()
	if envEnv := os.Getenv("GO_ENV"); envEnv != "" {
		env = envEnv
	}

	if env == "" {
		env = "dev" // 默认使用开发环境
	}

	var configPath string
	switch env {
	case "dev":
		configPath = "etc/file-api-uat-local.yaml"
	case "uat":
		configPath = "etc/file-api-uat.yaml"
	case "prd":
		configPath = "etc/file-api-prd.yaml"
	default:
		fmt.Println("Unknown environment:", env)
		return
	}
	// 获取当前时间
	now := time.Now()

	// 转换为 YYYY-MM-DD HH:mm:ss 格式
	formatted := now.Format("2006-01-02 15:04:05")

	// 输出格式化后的时间
	fmt.Println(formatted)

	fmt.Println("configPat:", configPath)
	var configFile = flag.String("f", configPath, "the config file")
	var c config.Config
	conf.MustLoad(*configFile, &c)

	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()
	//server.Use(middleware.MaxBytesMiddleware(10 << 20))
	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
